using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManagementApplication
{
    public partial class FrmMarks : Form
    {
        List<Entities.Course> courseList = new List<Entities.Course>();
        List<Entities.Class> classList = new List<Entities.Class>();
        List<Entities.Marks> marksList = new List<Entities.Marks>();
        Business.MarksService marksService = new Business.MarksService();
        Business.CourseService courseService = new Business.CourseService();
        Business.ClassService classService = new Business.ClassService();
        Entities.Marks marks = new Entities.Marks();
        int iIndex = 0;

        public FrmMarks()
        {
            InitializeComponent();
        }


        private void InitializeCourse()
        {
            courseList = courseService.GetAllCourses();
            foreach (Entities.Course course in courseList)
            {
                cmbCourse.Items.Add(course.CourseID + "--" + course.CourseName);
            }
        }

        private void InitializeClass()
        {
            cmbClass.Items.Clear();
            if (cmbCourse.Text.Length > 0)
            {
                classList = classService.GetClassByCourseID(cmbCourse.Text.Substring(0,5));
                foreach (Entities.Class _class in classList)
                {
                    cmbClass.Items.Add(_class.ClassID + "--" + _class.ClassName);
                }
            }
            else
            {
                classList = classService.GetAllClasses();
                foreach (Entities.Class _class in classList)
                {
                    cmbClass.Items.Add(_class.ClassID + "--" + _class.ClassName);
                }
            }
        }

        private void ShowDataGirdView()
        {

            if (cmbCourse.Text == string.Empty && cmbClass.Text != string.Empty)
            {
                using (DataTable dataTable = marksService.GetMarksByClassID(cmbClass.Text.Substring(0, 6)))
                {
                    ShowData(dataTable);
                }
            }
            else
                if (cmbCourse.Text != string.Empty && cmbClass.Text == string.Empty)
                {
                    using (DataTable dataTable = marksService.GetMarksByCourseID(cmbCourse.Text.Substring(0, 5)))
                    {
                        ShowData(dataTable);
                    }
                }
                else
                {
                    using (DataTable dataTable = marksService.GetMarksByCourseIDAndClassID(cmbCourse.Text.Substring(0, 5), cmbClass.Text.Substring(0, 6)))
                    {
                        ShowData(dataTable);
                    }
                }
            dgvMarks[0, iIndex].Selected = true;
        }

        private void ShowData(DataTable dataTable)
        {
            if (dgvMarks.Rows.Count > 1)
            {
                int iCount = dgvMarks.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvMarks.Rows.RemoveAt(0);
                }
            }
            foreach (DataRow row in dataTable.Rows)
            {
                string[] temp = { row[0].ToString(), row[1].ToString(), row[2].ToString() };
                dgvMarks.Rows.Add(temp);
            }
        }

        private void FindMarksByStudentID(string sStudentID)
        {
            using (DataTable dataTable = marksService.GetAllMarks2())
            {
                if (dgvMarks.Rows.Count > 1)
                {
                    int iCount = dgvMarks.Rows.Count - 1;
                    for (int i = 0; i < iCount; i++)
                    {
                        dgvMarks.Rows.RemoveAt(0);
                    }
                }
                foreach (DataRow row in dataTable.Rows)
                {
                    if (row[0].ToString().ToLower().Contains(sStudentID))
                    {
                        string[] temp = { row[0].ToString(), row[1].ToString(), row[2].ToString() };
                        dgvMarks.Rows.Add(temp);
                    }
                }
            }
            iIndex = 0;
            dgvMarks[0, iIndex].Selected = true;
            if (dgvMarks.Rows.Count > 1) FindStudentMarks(dgvMarks.Rows[iIndex].Cells[0].Value.ToString());
        }

        private void FindStudentMarks(string sStudentID)
        {
            marks = marksService.GetMarksByStudentID(sStudentID);
            txtStudentID.Text = marks.StudentID;
            txtMarks.Text = marks.Mark.ToString();
        }

        private bool Test()
        {
            bool bResult = true;
            string sInformation = string.Empty;
            if (txtMarks.Text == string.Empty)
                sInformation += "Marks is empty" + "\n";
            if (int.Parse(txtMarks.Text) < 0 && int.Parse(txtMarks.Text) > 10)
                sInformation += "Marks must be greater than 0 and not be greater than 10" + "\n";
            if (sInformation != string.Empty)
            {
                MessageBox.Show(sInformation, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bResult = false;
            }
            return bResult;
        }

        private void FrmMarks_Load(object sender, EventArgs e)
        {
            InitializeCourse();
            InitializeClass();
        }

        private void cmbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitializeClass();
            ShowDataGirdView();
        }

        private void cmbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowDataGirdView();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FindMarksByStudentID(txtStudentID.Text.ToLower());
        }

        private void dgvMarks_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvMarks.CurrentRow.Index < dgvMarks.Rows.Count - 1)
            {
                iIndex = dgvMarks.CurrentRow.Index;
                string sStudentID = dgvMarks.CurrentRow.Cells[0].Value.ToString();
                FindStudentMarks(sStudentID);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Test())
            {
                marks.Mark = int.Parse(txtMarks.Text);
                marksService.UpdateMarks(marks);
                ShowDataGirdView();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}